<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/blog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/blog/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.2/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"littlefxc.github.io","root":"/blog/","images":"/blog/images","scheme":"Mist","version":"8.2.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/blog/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}};
  </script>
<meta name="description" content="Spring SpEl 表达式语言 - 学习记录SpEL是类似于OGNL和JSF EL的表达式语言，能够在运行时构建复杂表达式，存取对象属性、对象方法调用等。所有的SpEL都支持XML和Annotation两种方式，格式：#&amp;#123; expressoin &amp;#125; Maven 依赖因为 spring-context 依赖中包含 spring-expression，所以选择它。 123456">
<meta property="og:type" content="article">
<meta property="og:title" content="spring-expression学习记录">
<meta property="og:url" content="http://littlefxc.github.io/2019/05/06/spring-expression%E5%AD%A6%E4%B9%A0%E8%AE%B0%E5%BD%95/index.html">
<meta property="og:site_name" content="一年春又来">
<meta property="og:description" content="Spring SpEl 表达式语言 - 学习记录SpEL是类似于OGNL和JSF EL的表达式语言，能够在运行时构建复杂表达式，存取对象属性、对象方法调用等。所有的SpEL都支持XML和Annotation两种方式，格式：#&amp;#123; expressoin &amp;#125; Maven 依赖因为 spring-context 依赖中包含 spring-expression，所以选择它。 123456">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2019-05-06T12:13:40.000Z">
<meta property="article:modified_time" content="2021-03-25T13:15:49.036Z">
<meta property="article:author" content="一年春又来">
<meta property="article:tag" content="spring">
<meta property="article:tag" content="spring-expression">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://littlefxc.github.io/2019/05/06/spring-expression%E5%AD%A6%E4%B9%A0%E8%AE%B0%E5%BD%95/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>
<title>spring-expression学习记录 | 一年春又来</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

<link rel="alternate" href="/blog/atom.xml" title="一年春又来" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/blog/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">一年春又来</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/blog/" rel="section"><i class="home                          //首页 fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-archives"><a href="/blog/archives/" rel="section"><i class="archive          //归档 fa-fw"></i>归档</a></li>
        <li class="menu-item menu-item-categories"><a href="/blog/categories/" rel="section"><i class="th           //分类 fa-fw"></i>分类</a></li>
        <li class="menu-item menu-item-tags"><a href="/blog/tags/" rel="section"><i class="tags                     //标签 fa-fw"></i>标签</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Spring-SpEl-%E8%A1%A8%E8%BE%BE%E5%BC%8F%E8%AF%AD%E8%A8%80-%E5%AD%A6%E4%B9%A0%E8%AE%B0%E5%BD%95"><span class="nav-text">Spring SpEl 表达式语言 - 学习记录</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#Maven-%E4%BE%9D%E8%B5%96"><span class="nav-text">Maven 依赖</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="nav-text">初始化</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%85%A5%E9%97%A8"><span class="nav-text">1. 入门</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%94%AF%E6%8C%81%E6%89%A7%E8%A1%8C%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%A1%A8%E8%BE%BE%E5%BC%8F"><span class="nav-text">支持执行字符串表达式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#SpEL%E6%94%AF%E6%8C%81%E5%B9%BF%E6%B3%9B%E7%9A%84%E5%8A%9F%E8%83%BD%EF%BC%8C%E4%BE%8B%E5%A6%82%E8%B0%83%E7%94%A8%E6%96%B9%E6%B3%95%EF%BC%8C%E8%AE%BF%E9%97%AE%E5%B1%9E%E6%80%A7%E5%92%8C%E8%B0%83%E7%94%A8%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0"><span class="nav-text">SpEL支持广泛的功能，例如调用方法，访问属性和调用构造函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#SpEL%E7%9A%84%E6%9B%B4%E5%B8%B8%E8%A7%81%E7%94%A8%E6%B3%95%E6%98%AF%E6%8F%90%E4%BE%9B%E9%92%88%E5%AF%B9%E7%89%B9%E5%AE%9A%E5%AF%B9%E8%B1%A1%E5%AE%9E%E4%BE%8B%EF%BC%88%E7%A7%B0%E4%B8%BA%E6%A0%B9%E5%AF%B9%E8%B1%A1%EF%BC%89%E8%AE%A1%E7%AE%97%E7%9A%84%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="nav-text">SpEL的更常见用法是提供针对特定对象实例（称为根对象）计算的表达式字符串</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Spring-Beans-%E4%B8%AD%E7%9A%84-SpEl-%E8%A1%A8%E8%BE%BE%E5%BC%8F-Bean-%E7%9A%84%E5%BC%95%E7%94%A8"><span class="nav-text">Spring Beans 中的 SpEl 表达式, Bean 的引用</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%9C%A8-Spring-%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E4%B8%AD%E4%BD%BF%E7%94%A8-SpEl-%E8%A1%A8%E8%BE%BE%E5%BC%8F"><span class="nav-text">在 Spring 配置文件中使用 SpEl 表达式</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%9C%A8%E6%B3%A8%E8%A7%A3%E4%B8%AD%E7%94%A8%E6%B3%951%EF%BC%9A%E5%9C%A8%E6%88%90%E5%91%98%E5%B1%9E%E6%80%A7%E4%B8%8A%E4%BD%BF%E7%94%A8-SpEL-%E8%A1%A8%E8%BE%BE%E5%BC%8F"><span class="nav-text">在注解中用法1：在成员属性上使用 SpEL 表达式</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%9C%A8%E6%B3%A8%E8%A7%A3%E4%B8%AD%E7%94%A8%E6%B3%952%EF%BC%9A%E5%9C%A8%E6%9E%84%E9%80%A0%E6%96%B9%E6%B3%95%E4%B8%8A%E4%BD%BF%E7%94%A8-SpEL-%E8%A1%A8%E8%BE%BE%E5%BC%8F"><span class="nav-text">在注解中用法2：在构造方法上使用 SpEL 表达式</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%9C%A8%E6%B3%A8%E8%A7%A3%E4%B8%AD%E7%94%A8%E6%B3%953%EF%BC%9A%E5%9C%A8%E5%B1%9E%E6%80%A7%E8%AE%BE%E7%BD%AE%E6%96%B9%E6%B3%95%E4%B8%8A%E4%BD%BF%E7%94%A8-SpEL-%E8%A1%A8%E8%BE%BE%E5%BC%8F"><span class="nav-text">在注解中用法3：在属性设置方法上使用 SpEL 表达式</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%B5%8B%E8%AF%95"><span class="nav-text">测试</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%AD%97%E9%9D%A2%E9%87%8F%E8%A1%A8%E8%BE%BE%E5%BC%8F"><span class="nav-text">2. 字面量表达式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E9%9B%86%E5%90%88%E7%9A%84%E8%A1%A8%E8%BE%BE%E5%BC%8F-Properties-Arrays-Lists-Maps-and-Indexers"><span class="nav-text">3. 集合的表达式(Properties, Arrays, Lists, Maps, and Indexers)</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E5%86%85%E8%81%94%E5%88%97%E8%A1%A8-Inline-Lists"><span class="nav-text">4. 内联列表( Inline Lists )</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-%E5%86%85%E8%81%94%E5%AD%97%E5%85%B8-Inline-Maps"><span class="nav-text">5. 内联字典( Inline Maps )</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-%E6%95%B0%E7%BB%84%E6%9E%84%E9%80%A0-Array-Construction"><span class="nav-text">6. 数组构造(Array Construction)</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#8-%E6%96%B9%E6%B3%95%E8%B0%83%E7%94%A8"><span class="nav-text">8. 方法调用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#9-%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-text">9. 运算符</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#9-1-%E5%85%B3%E7%B3%BB%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-text">9.1. 关系运算符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#9-2-%E9%80%BB%E8%BE%91%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-text">9.2. 逻辑运算符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#9-3-%E8%B5%8B%E5%80%BC%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-text">9.3. 赋值运算符</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#10-%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0"><span class="nav-text">10. 构造函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#11-%E5%8F%98%E9%87%8F%E5%AE%9A%E4%B9%89%E5%8F%8A%E5%BC%95%E7%94%A8"><span class="nav-text">11. 变量定义及引用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#12-%E8%87%AA%E5%AE%9A%E4%B9%89%E5%87%BD%E6%95%B0"><span class="nav-text">12. 自定义函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#13-%E4%B8%89%E7%9B%AE%E8%BF%90%E7%AE%97%E5%8F%8AElivis%E8%BF%90%E7%AE%97%E8%A1%A8%E8%BE%BE%E5%BC%8F"><span class="nav-text">13. 三目运算及Elivis运算表达式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#14-%E5%AE%89%E5%85%A8%E7%9A%84%E5%AF%BC%E8%88%AA%E6%93%8D%E4%BD%9C%E7%AC%A6"><span class="nav-text">14. 安全的导航操作符</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#15-%E9%9B%86%E5%90%88%E9%80%89%E6%8B%A9-Collection-Selection"><span class="nav-text">15. 集合选择(Collection Selection)</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#16-%E9%9B%86%E5%90%88%E6%8A%95%E5%BD%B1-Collection-Projection"><span class="nav-text">16. 集合投影(Collection Projection)</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#17-%E5%BC%95%E7%94%A8"><span class="nav-text">17. 引用</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">一年春又来</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">184</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/blog/categories/">
          
        <span class="site-state-item-count">35</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/blog/tags/">
          
        <span class="site-state-item-count">115</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2019/05/06/spring-expression%E5%AD%A6%E4%B9%A0%E8%AE%B0%E5%BD%95/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          spring-expression学习记录
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2019-05-06 20:13:40" itemprop="dateCreated datePublished" datetime="2019-05-06T20:13:40+08:00">2019-05-06</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-03-25 21:15:49" itemprop="dateModified" datetime="2021-03-25T21:15:49+08:00">2021-03-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/blog/categories/Java/" itemprop="url" rel="index"><span itemprop="name">Java</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="Spring-SpEl-表达式语言-学习记录"><a href="#Spring-SpEl-表达式语言-学习记录" class="headerlink" title="Spring SpEl 表达式语言 - 学习记录"></a>Spring SpEl 表达式语言 - 学习记录</h1><p>SpEL是类似于OGNL和JSF EL的表达式语言，能够在运行时构建复杂表达式，存取对象属性、对象方法调用等。<br>所有的SpEL都支持XML和Annotation两种方式，格式：<code>#&#123; expressoin &#125;</code></p>
<h2 id="Maven-依赖"><a href="#Maven-依赖" class="headerlink" title="Maven 依赖"></a>Maven 依赖</h2><p>因为 <code>spring-context</code> 依赖中包含 <code>spring-expression</code>，所以选择它。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-context<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="初始化"><a href="#初始化" class="headerlink" title="初始化"></a>初始化</h2><p>在后面的一系列的测试中，都有用到在 <code>@Before</code> 中的元素。我仅仅只是将一份这份单元测试类分解了而已。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SpelTest</span> </span>&#123;</span><br><span class="line">    ExpressionParser parser;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 测试 XML 配置 SpEl</span></span><br><span class="line">    User user;</span><br><span class="line"></span><br><span class="line">    BeanFactory beanFactory;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 测试注解配置 SpEl</span></span><br><span class="line">    FieldValueTestBean fieldValueTestBean;</span><br><span class="line"></span><br><span class="line">    MovieRecommender movieRecommender;</span><br><span class="line"></span><br><span class="line">    PropertyValueTestBean propertyValueTestBean;</span><br><span class="line"></span><br><span class="line">    StandardEvaluationContext context;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 发明家列表</span></span><br><span class="line">    List&lt;Inventor&gt; inventorList = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 发明家特斯拉</span></span><br><span class="line">    Inventor inventorTesla;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 发明家爱迪生</span></span><br><span class="line">    Inventor inventorEdlson;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 发明家贝尔</span></span><br><span class="line">    Inventor inventorBell;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 发明家数组</span></span><br><span class="line">    Inventor[] inventorArrays;</span><br><span class="line"></span><br><span class="line">    Society society;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 初始化Spring</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Before</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setUp</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        beanFactory = <span class="keyword">new</span> ClassPathXmlApplicationContext(<span class="string">&quot;classpath:spring-root.xml&quot;</span>);</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="comment">// 允许 SpEl 表达式访问 IOC 容器中的 bean</span></span><br><span class="line">            <span class="comment">// SpEL支持使用 &quot;@&quot; 符号来引用Bean, 在引用Bean时需要使用BeanResolver接口实现来查找Bean, Spring提供BeanFactoryResolver实现</span></span><br><span class="line">            context = <span class="keyword">new</span> StandardEvaluationContext();</span><br><span class="line">            context.setBeanResolver(<span class="keyword">new</span> BeanFactoryResolver(beanFactory));</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// SpEl 解析器</span></span><br><span class="line">        parser = (ExpressionParser) beanFactory.getBean(<span class="string">&quot;parser&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// XML 配置 SpEl</span></span><br><span class="line">        user = (User) beanFactory.getBean(<span class="string">&quot;user&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 注解配置 SpEl</span></span><br><span class="line">        fieldValueTestBean = (FieldValueTestBean) beanFactory.getBean(<span class="string">&quot;fieldValueTestBean&quot;</span>);</span><br><span class="line">        movieRecommender = (MovieRecommender) beanFactory.getBean(<span class="string">&quot;movieRecommender&quot;</span>);</span><br><span class="line">        propertyValueTestBean = (PropertyValueTestBean) beanFactory.getBean(<span class="string">&quot;propertyValueTestBean&quot;</span>);</span><br><span class="line"></span><br><span class="line">        &#123;</span><br><span class="line">            <span class="comment">// 设置 SpEl 的 根对象</span></span><br><span class="line">            inventorTesla = <span class="keyword">new</span> Inventor(<span class="string">&quot;尼古拉·特斯拉&quot;</span>, <span class="string">&quot;塞尔维亚裔美籍&quot;</span>);</span><br><span class="line">            inventorTesla.setPlaceOfBirth(<span class="keyword">new</span> PlaceOfBirth(<span class="string">&quot;利卡-塞尼县&quot;</span>, <span class="string">&quot;克罗地亚&quot;</span>));</span><br><span class="line"></span><br><span class="line">            inventorEdlson = <span class="keyword">new</span> Inventor(<span class="string">&quot;托马斯·阿尔瓦·爱迪生&quot;</span>, <span class="string">&quot;美国&quot;</span>);</span><br><span class="line">            inventorEdlson.setPlaceOfBirth(<span class="keyword">new</span> PlaceOfBirth(<span class="string">&quot;米兰&quot;</span>, <span class="string">&quot;美国俄亥俄州&quot;</span>));</span><br><span class="line"></span><br><span class="line">            inventorBell = <span class="keyword">new</span> Inventor(<span class="string">&quot;亚历山大·格拉汉姆·贝尔&quot;</span>, <span class="string">&quot;美国&quot;</span>);</span><br><span class="line">            inventorBell.setPlaceOfBirth(<span class="keyword">new</span> PlaceOfBirth(<span class="string">&quot;爱丁堡&quot;</span>, <span class="string">&quot;英国苏格兰&quot;</span>));</span><br><span class="line"></span><br><span class="line">            inventorList.add(inventorTesla);</span><br><span class="line">            inventorList.add(inventorEdlson);</span><br><span class="line">            inventorList.add(inventorBell);</span><br><span class="line"></span><br><span class="line">            <span class="comment">// 数组和列表的内容通过使用方括号表示法获得</span></span><br><span class="line">            Inventor[] inventorArrays = <span class="keyword">new</span> Inventor[<span class="number">3</span>];</span><br><span class="line">            inventorArrays = inventorList.toArray(inventorArrays);</span><br><span class="line"></span><br><span class="line">            society = <span class="keyword">new</span> Society();</span><br><span class="line">            society.getOfficers().put(Society.President, inventorEdlson);</span><br><span class="line">            society.getOfficers().put(Society.Advisors, inventorList);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="1-入门"><a href="#1-入门" class="headerlink" title="1. 入门"></a>1. 入门</h2><h3 id="支持执行字符串表达式"><a href="#支持执行字符串表达式" class="headerlink" title="支持执行字符串表达式"></a>支持执行字符串表达式</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test1</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    ExpressionParser parser = <span class="keyword">new</span> SpelExpressionParser();</span><br><span class="line">    Expression exp = parser.parseExpression(<span class="string">&quot;&#x27;Hello World&#x27;&quot;</span>);</span><br><span class="line">    String message = (String) exp.getValue();</span><br><span class="line">    Assert.assertEquals(<span class="string">&quot;Hello World&quot;</span>, message);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="SpEL支持广泛的功能，例如调用方法，访问属性和调用构造函数"><a href="#SpEL支持广泛的功能，例如调用方法，访问属性和调用构造函数" class="headerlink" title="SpEL支持广泛的功能，例如调用方法，访问属性和调用构造函数"></a>SpEL支持广泛的功能，例如调用方法，访问属性和调用构造函数</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test2</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    ExpressionParser parser = <span class="keyword">new</span> SpelExpressionParser();</span><br><span class="line">    <span class="comment">// 调用方法</span></span><br><span class="line">    Expression exp = parser.parseExpression(<span class="string">&quot;&#x27;Hello World&#x27;.concat(&#x27;!&#x27;)&quot;</span>);</span><br><span class="line">    String message = (String) exp.getValue();</span><br><span class="line">    Assert.assertEquals(<span class="string">&quot;Hello World!&quot;</span>, message);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 调用属性</span></span><br><span class="line">    exp = parser.parseExpression(<span class="string">&quot;&#x27;Hello World&#x27;.bytes&quot;</span>);</span><br><span class="line">    <span class="keyword">byte</span>[] bytesE = <span class="string">&quot;Hello World&quot;</span>.getBytes();</span><br><span class="line">    <span class="keyword">byte</span>[] bytesA = (<span class="keyword">byte</span>[]) exp.getValue();</span><br><span class="line">    Assert.assertArrayEquals(bytesE, bytesA);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// SpEL还通过使用标准点表示法（例如prop1.prop2.prop3）和属性值的设置来支持嵌套属性。也可以访问公共字段。</span></span><br><span class="line">    exp = parser.parseExpression(<span class="string">&quot;&#x27;Hello World&#x27;.bytes.length&quot;</span>);</span><br><span class="line">    <span class="keyword">int</span> lengthE = <span class="string">&quot;Hello World&quot;</span>.getBytes().length;</span><br><span class="line">    <span class="keyword">int</span> lengthA = (<span class="keyword">int</span>) exp.getValue();</span><br><span class="line">    Assert.assertEquals(lengthE, lengthA);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 调用构造方法</span></span><br><span class="line">    exp = parser.parseExpression(<span class="string">&quot;new String(&#x27;hello world&#x27;).toUpperCase()&quot;</span>);</span><br><span class="line">    String constructorA = exp.getValue(String.class);</span><br><span class="line">    Assert.assertEquals(<span class="string">&quot;HELLO WORLD&quot;</span>, constructorA);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h3 id="SpEL的更常见用法是提供针对特定对象实例（称为根对象）计算的表达式字符串"><a href="#SpEL的更常见用法是提供针对特定对象实例（称为根对象）计算的表达式字符串" class="headerlink" title="SpEL的更常见用法是提供针对特定对象实例（称为根对象）计算的表达式字符串"></a>SpEL的更常见用法是提供针对特定对象实例（称为根对象）计算的表达式字符串</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test3</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    ExpressionParser parser = <span class="keyword">new</span> SpelExpressionParser();</span><br><span class="line"></span><br><span class="line">    Expression exp = parser.parseExpression(<span class="string">&quot;username == &#x27;fxc&#x27;&quot;</span>);</span><br><span class="line">    Boolean expValue = exp.getValue(user, Boolean.class);</span><br><span class="line">    Assert.assertEquals(<span class="keyword">true</span>, expValue);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="Spring-Beans-中的-SpEl-表达式-Bean-的引用"><a href="#Spring-Beans-中的-SpEl-表达式-Bean-的引用" class="headerlink" title="Spring Beans 中的 SpEl 表达式, Bean 的引用"></a>Spring Beans 中的 SpEl 表达式, Bean 的引用</h3><p>Spring SpEL 可以在 XML 配置文件中使用，同样可以在注解配置中使用例如 <code>@Value(&quot;#&#123;expression&#125;&quot;)</code>。</p>
<p>Spring 允许 SpEl 表达式访问 IOC 容器中的 bean，SpEL支持使用 <code>@</code> 符号来引用Bean, 在引用 Bean 时需要使用 <code>BeanResolver</code> 接口实现来查找Bean, Spring提供 <code>BeanFactoryResolver</code> 实现</p>
<p>ClassPathXmlApplicationContext 实现默认会把 <code>System.getProperties()</code> 注册名为<code>systemProperties</code> 的 Bean，因此使用 <code>@systemProperties</code> 来引用该 Bean。</p>
<h4 id="在-Spring-配置文件中使用-SpEl-表达式"><a href="#在-Spring-配置文件中使用-SpEl-表达式" class="headerlink" title="在 Spring 配置文件中使用 SpEl 表达式"></a>在 Spring 配置文件中使用 SpEl 表达式</h4><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">beans</span> <span class="attr">xmlns</span>=<span class="string">&quot;http://www.springframework.org/schema/beans&quot;</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:xsi</span>=<span class="string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:context</span>=<span class="string">&quot;http://www.springframework.org/schema/context&quot;</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xsi:schemaLocation</span>=<span class="string">&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd&quot;</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">context:property-placeholder</span> <span class="attr">location</span>=<span class="string">&quot;classpath:application.properties&quot;</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">context:component-scan</span> <span class="attr">base-package</span>=<span class="string">&quot;com.littlefxc.examples.spel&quot;</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;parser&quot;</span> <span class="attr">class</span>=<span class="string">&quot;org.springframework.expression.spel.standard.SpelExpressionParser&quot;</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- 同样也适用于 Java Config, 例如 @Value(&quot;#&#123;systemProperties[&#x27;user.country&#x27;]&#125;&quot;) --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;user&quot;</span> <span class="attr">class</span>=<span class="string">&quot;com.littlefxc.examples.spel.User&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;username&quot;</span> <span class="attr">value</span>=<span class="string">&quot;fxc&quot;</span>/&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--&lt;property name=&quot;uuid&quot; value=&quot;#&#123;T(java.util.UUID).randomUUID().toString()&#125;&quot;/&gt;--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;uuid&quot;</span> <span class="attr">value</span>=<span class="string">&quot;$&#123;user.uuid&#125;&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;place&quot;</span> <span class="attr">value</span>=<span class="string">&quot;#&#123;systemProperties[&#x27;user.country&#x27;]&#125;&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;age&quot;</span> <span class="attr">value</span>=<span class="string">&quot;$&#123;user.age&#125;&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">beans</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>application.properties</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">user.uuid=#&#123;T(java.util.UUID).randomUUID().toString()&#125;</span></span><br><span class="line"><span class="string">user.age=18</span></span><br></pre></td></tr></table></figure>

<h4 id="在注解中用法1：在成员属性上使用-SpEL-表达式"><a href="#在注解中用法1：在成员属性上使用-SpEL-表达式" class="headerlink" title="在注解中用法1：在成员属性上使用 SpEL 表达式"></a>在注解中用法1：在成员属性上使用 SpEL 表达式</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.littlefxc.examples.spel;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Value;</span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Component;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> fengxuechao</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span> 2019/1/24</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">FieldValueTestBean</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Value(&quot;#&#123; systemProperties[&#x27;user.country&#x27;] &#125;&quot;)</span></span><br><span class="line">    <span class="keyword">private</span> String defaultLocale;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 省略 setter getter</span></span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="在注解中用法2：在构造方法上使用-SpEL-表达式"><a href="#在注解中用法2：在构造方法上使用-SpEL-表达式" class="headerlink" title="在注解中用法2：在构造方法上使用 SpEL 表达式"></a>在注解中用法2：在构造方法上使用 SpEL 表达式</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MovieRecommender</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> String movieName;</span><br><span class="line">    <span class="keyword">private</span> String defaultLocale;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">MovieRecommender</span><span class="params">(<span class="meta">@Value(&quot;#&#123; systemProperties[&#x27;user.country&#x27;] &#125;&quot;)</span> String defaultLocale)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.defaultLocale = defaultLocale;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 省略 setter getter</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="在注解中用法3：在属性设置方法上使用-SpEL-表达式"><a href="#在注解中用法3：在属性设置方法上使用-SpEL-表达式" class="headerlink" title="在注解中用法3：在属性设置方法上使用 SpEL 表达式"></a>在注解中用法3：在属性设置方法上使用 SpEL 表达式</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.littlefxc.examples.spel;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Value;</span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Component;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">PropertyValueTestBean</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> String defaultLocale;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getDefaultLocale</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.defaultLocale;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Value(&quot;#&#123; systemProperties[&#x27;user.country&#x27;] &#125;&quot;)</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setDefaultLocale</span><span class="params">(String defaultLocale)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.defaultLocale = defaultLocale;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="测试"><a href="#测试" class="headerlink" title="测试"></a>测试</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test4</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 允许 SpEl 表达式访问 IOC 容器中的 bean</span></span><br><span class="line">    <span class="comment">// SpEL支持使用 &quot;@&quot; 符号来引用Bean, 在引用Bean时需要使用BeanResolver接口实现来查找Bean, Spring提供BeanFactoryResolver实现</span></span><br><span class="line">    <span class="comment">// ClassPathXmlApplicationContext 实现默认会把&quot;System.getProperties()&quot;注册为&quot;systemProperties&quot;Bean，因此我们使用 &quot;@systemProperties&quot;来引用该Bean</span></span><br><span class="line">    Properties result1 = parser.parseExpression(<span class="string">&quot;@systemProperties&quot;</span>).getValue(context, Properties.class);</span><br><span class="line">    System.out.println(result1.getProperty(<span class="string">&quot;user.dir&quot;</span>, <span class="string">&quot;环境变量中没有该属性&quot;</span>));</span><br><span class="line"></span><br><span class="line">    <span class="comment">// XML 配置</span></span><br><span class="line">    User userActual = parser.parseExpression(<span class="string">&quot;@user&quot;</span>).getValue(context, User.class);</span><br><span class="line">    Assert.assertSame(user, userActual);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 注解配置</span></span><br><span class="line">    Assert.assertSame(movieRecommender, parser.parseExpression(<span class="string">&quot;@movieRecommender&quot;</span>).getValue(context, MovieRecommender.class));</span><br><span class="line">    Assert.assertSame(propertyValueTestBean, parser.parseExpression(<span class="string">&quot;@propertyValueTestBean&quot;</span>).getValue(context, PropertyValueTestBean.class));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="2-字面量表达式"><a href="#2-字面量表达式" class="headerlink" title="2. 字面量表达式"></a>2. 字面量表达式</h2><p>SpEL支持的字面量包括：字符串、数字类型（int、long、float、double）、布尔类型、null类型。</p>
<table>
<thead>
<tr>
<th>类型</th>
<th>示例</th>
</tr>
</thead>
<tbody><tr>
<td>字符串</td>
<td>String str1 = parser.parseExpression(“‘Hello World!’”).getValue(String.class); <br> String str2 = parser.parseExpression(“\“Hello World!\“”).getValue(String.class);</td>
</tr>
<tr>
<td>数字类型</td>
<td>int int1 = parser.parseExpression(“1”).getValue(Integer.class); <br> long long1 = parser.parseExpression(“-1L”).getValue(long.class); <br> float float1 = parser.parseExpression(“1.1”).getValue(Float.class); <br> double double1 = parser.parseExpression(“1.1E+2”).getValue(double.class); <br> int hex1 = parser.parseExpression(“0xa”).getValue(Integer.class); <br> long hex2 = parser.parseExpression(“0xaL”).getValue(long.class);</td>
</tr>
<tr>
<td>布尔类型</td>
<td>boolean true1 = parser.parseExpression(“true”).getValue(boolean.class); <br> boolean false1 = parser.parseExpression(“false”).getValue(boolean.class);</td>
</tr>
<tr>
<td>null类型</td>
<td>Object null1 = parser.parseExpression(“null”).getValue(Object.class);</td>
</tr>
</tbody></table>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test5</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    ExpressionParser parser = <span class="keyword">new</span> SpelExpressionParser();</span><br><span class="line"></span><br><span class="line">    <span class="comment">// evals to &quot;Hello World&quot;</span></span><br><span class="line">    String helloWorld = (String) parser.parseExpression(<span class="string">&quot;&#x27;Hello World&#x27;&quot;</span>).getValue();</span><br><span class="line">    Assert.assertEquals(<span class="string">&quot;Hello World&quot;</span>, helloWorld);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 浮点型字面量</span></span><br><span class="line">    <span class="keyword">double</span> avogadrosNumber = (Double) parser.parseExpression(<span class="string">&quot;6.0221415E+23&quot;</span>).getValue();</span><br><span class="line">    Assert.assertEquals(Double.valueOf(<span class="string">&quot;6.0221415E+23&quot;</span>), avogadrosNumber, Double.MIN_VALUE);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// evals to 2147483647</span></span><br><span class="line">    <span class="keyword">int</span> maxValue = (Integer) parser.parseExpression(<span class="string">&quot;0x7FFFFFFF&quot;</span>).getValue();</span><br><span class="line">    Assert.assertEquals(<span class="number">2147483647</span>, maxValue);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">boolean</span> trueValue = (Boolean) parser.parseExpression(<span class="string">&quot;true&quot;</span>).getValue();</span><br><span class="line">    Assert.assertTrue(trueValue);</span><br><span class="line"></span><br><span class="line">    Object nullValue = parser.parseExpression(<span class="string">&quot;null&quot;</span>).getValue();</span><br><span class="line">    Assert.assertNull(nullValue);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="3-集合的表达式-Properties-Arrays-Lists-Maps-and-Indexers"><a href="#3-集合的表达式-Properties-Arrays-Lists-Maps-and-Indexers" class="headerlink" title="3. 集合的表达式(Properties, Arrays, Lists, Maps, and Indexers)"></a>3. 集合的表达式(Properties, Arrays, Lists, Maps, and Indexers)</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 集合的表达式</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@see</span> java.util.Properties</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@see</span> java.util.Arrays</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@see</span> java.util.List</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@see</span> java.util.Map</span></span><br><span class="line"><span class="comment">     * Index 索引</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test6</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 属性名称的第一个字母不区分大小写</span></span><br><span class="line">        <span class="comment">// 数组和列表的内容通过使用方括号表示法获得</span></span><br><span class="line">        <span class="keyword">int</span> year = (Integer) parser.parseExpression(<span class="string">&quot;Birthdate.Year = 1856&quot;</span>).getValue(context);</span><br><span class="line">        String city = (String) parser.parseExpression(<span class="string">&quot;placeOfBirth.City&quot;</span>).getValue(context);</span><br><span class="line">        Assert.assertEquals(<span class="number">1856</span>, year);</span><br><span class="line">        Assert.assertEquals(<span class="string">&quot;利卡-塞尼县&quot;</span>, city);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 列表</span></span><br><span class="line">        context.setRootObject(inventorList);</span><br><span class="line">        String birthCity = parser</span><br><span class="line">                .parseExpression(<span class="string">&quot;[0].placeOfBirth.City&quot;</span>)</span><br><span class="line">                .getValue(context, String.class);</span><br><span class="line">        Assert.assertEquals(<span class="string">&quot;利卡-塞尼县&quot;</span>, birthCity);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 字典</span></span><br><span class="line">        context.setRootObject(society);</span><br><span class="line">        <span class="comment">// 改变字典的值的某个属性</span></span><br><span class="line">        parser.parseExpression(<span class="string">&quot;Officers[&#x27;president&#x27;].placeOfBirth.City&quot;</span>)</span><br><span class="line">                .setValue(context, <span class="string">&quot;随便&quot;</span>);</span><br><span class="line">        <span class="comment">// 获取字典的值的某个属性</span></span><br><span class="line">        birthCity = (String) parser</span><br><span class="line">                .parseExpression(<span class="string">&quot;officers[&#x27;president&#x27;].placeOfBirth.City&quot;</span>)</span><br><span class="line">                .getValue(context);</span><br><span class="line">        Assert.assertEquals(<span class="string">&quot;随便&quot;</span>, birthCity);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h2 id="4-内联列表-Inline-Lists"><a href="#4-内联列表-Inline-Lists" class="headerlink" title="4. 内联列表( Inline Lists )"></a>4. 内联列表( Inline Lists )</h2><p>可以使用{}符号直接在表达式中表示列表.{}本身表示一个空列表。出于性能原因，如果列表本身完全由固定的文字组成，那么将创建一个常量列表来表示表达式(而不是在每个表达式上构建一个新的列表)</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 内联列表(Inline Lists)</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Test(expected = UnsupportedOperationException.class)</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test7_1</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 将返回不可修改的空List</span></span><br><span class="line">    List list = parser.parseExpression(<span class="string">&quot;&#123;&#125;&quot;</span>).getValue(List.class);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 对于字面量列表也将返回不可修改的List</span></span><br><span class="line">    List list2 = parser.parseExpression(<span class="string">&quot;&#123;1,2,3,4&#125;&quot;</span>).getValue(List.class);</span><br><span class="line">    <span class="comment">// 不会进行不可修改处理</span></span><br><span class="line">    list2.set(<span class="number">0</span>, <span class="number">2</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 内联列表(Inline Lists)</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test7_2</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 对于列表中只要有一个不是字面量表达式，将只返回原始List</span></span><br><span class="line">    List&lt;List&lt;Integer&gt;&gt; list = parser.parseExpression(<span class="string">&quot;&#123;&#123;1+2, 2+4&#125;,&#123;3, 4+4&#125;&#125;&quot;</span>).getValue(List.class);</span><br><span class="line">    <span class="comment">// 操作的不是原始列表</span></span><br><span class="line">    list.get(<span class="number">0</span>).set(<span class="number">0</span>, <span class="number">1</span>);</span><br><span class="line">    Integer value = parser.parseExpression(<span class="string">&quot;&#123;&#123;1+2, 2+4&#125;,&#123;3, 4+4&#125;&#125;[0][0]&quot;</span>).getValue(Integer.class);</span><br><span class="line">    Assert.assertEquals(<span class="number">1</span>, list.get(<span class="number">0</span>).get(<span class="number">0</span>).intValue());</span><br><span class="line">    Assert.assertEquals(<span class="number">3</span>, value.intValue());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="5-内联字典-Inline-Maps"><a href="#5-内联字典-Inline-Maps" class="headerlink" title="5. 内联字典( Inline Maps )"></a>5. 内联字典( Inline Maps )</h2><p>您可以使用熟悉的Java语法来构建数组，也可以提供一个初始化器来在构建时填充数组。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">    * 内联字典(Inline Maps)</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">   <span class="meta">@Test</span></span><br><span class="line">   <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test8</span><span class="params">()</span> </span>&#123;</span><br><span class="line">       <span class="comment">// evaluates to a Java map containing the two entries</span></span><br><span class="line">       Map inventorInfo = (Map) parser.parseExpression(<span class="string">&quot;&#123;name:&#x27;Nikola&#x27;,dob:&#x27;10-July-1856&#x27;&#125;&quot;</span>).getValue(context);</span><br><span class="line">       Assert.assertEquals(<span class="string">&quot;Nikola&quot;</span>, inventorInfo.get(<span class="string">&quot;name&quot;</span>));</span><br><span class="line"></span><br><span class="line">       <span class="comment">// 字典中的字典</span></span><br><span class="line">       Map mapOfMaps = (Map) parser</span><br><span class="line">               .parseExpression(<span class="string">&quot;&#123;name:&#123;first:&#x27;Nikola&#x27;,last:&#x27;Tesla&#x27;&#125;,dob:&#123;day:10,month:&#x27;July&#x27;,year:1856&#125;&#125;&quot;</span>)</span><br><span class="line">               .getValue(context);</span><br><span class="line">       Map tesla = (Map) mapOfMaps.get(<span class="string">&quot;name&quot;</span>);</span><br><span class="line">       Assert.assertEquals(<span class="string">&quot;Tesla&quot;</span>, tesla.get(<span class="string">&quot;last&quot;</span>));</span><br><span class="line"></span><br><span class="line">       <span class="comment">// 空字典 &#123;:&#125;</span></span><br><span class="line">       Map emptyMap = (Map) parser.parseExpression(<span class="string">&quot;&#123;:&#125;&quot;</span>).getValue(context);</span><br><span class="line">       Assert.assertEquals(<span class="number">0</span>, emptyMap.size());</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>

<p>{:}本身表示一个空映射。出于性能原因，如果映射本身由固定的文字或其他嵌套的常量结构(列表或映射)组成，则创建常量映射来表示表达式(而不是在每个表达式上构建新的映射)。</p>
<h2 id="6-数组构造-Array-Construction"><a href="#6-数组构造-Array-Construction" class="headerlink" title="6. 数组构造(Array Construction)"></a>6. 数组构造(Array Construction)</h2><p>您可以使用熟悉的Java语法来构建数组，也可以提供一个初始化器来在构建时填充数组</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 数组创建(Array Construction):相似的 Java 数组语法</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test9</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span>[] numbers1 = (<span class="keyword">int</span>[]) parser.parseExpression(<span class="string">&quot;new int[4]&quot;</span>).getValue(context);</span><br><span class="line">        Assert.assertArrayEquals(<span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">4</span>], numbers1);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// Array with initializer</span></span><br><span class="line">        <span class="keyword">int</span>[] numbers2 = (<span class="keyword">int</span>[]) parser.parseExpression(<span class="string">&quot;new int[]&#123;1,2,3&#125;&quot;</span>).getValue(context);</span><br><span class="line">        Assert.assertArrayEquals(<span class="keyword">new</span> <span class="keyword">int</span>[]&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>&#125;, numbers2);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// Multi dimensional array</span></span><br><span class="line">        <span class="keyword">int</span>[][] numbers3 = (<span class="keyword">int</span>[][]) parser.parseExpression(<span class="string">&quot;new int[4][5]&quot;</span>).getValue(context);</span><br><span class="line">        Assert.assertArrayEquals(<span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">4</span>][<span class="number">5</span>], numbers3);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<p>在构造多维数组时，无法初始化，也就是 <code>&quot;new int[4][5]&quot;</code> 后面无法添加<code>&#123;&#125;</code></p>
<h2 id="8-方法调用"><a href="#8-方法调用" class="headerlink" title="8. 方法调用"></a>8. 方法调用</h2><p>您可以使用典型的Java编程语法来调用方法。您还可以对文字调用方法。还支持变量参数。下面的示例展示了如何调用方法：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">    * 方法调用</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">   <span class="meta">@Test</span></span><br><span class="line">   <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test10</span><span class="params">()</span> </span>&#123;</span><br><span class="line">       <span class="comment">// string literal, evaluates to &quot;bc&quot;</span></span><br><span class="line">       String bc = parser.parseExpression(<span class="string">&quot;&#x27;abc&#x27;.substring(1, 3)&quot;</span>).getValue(String.class);</span><br><span class="line">       Assert.assertEquals(<span class="string">&quot;bc&quot;</span>, bc);</span><br><span class="line"></span><br><span class="line">       society.getMembers().add(inventorTesla);</span><br><span class="line">       context.setRootObject(society);</span><br><span class="line">       <span class="comment">// evaluates to true</span></span><br><span class="line">       <span class="keyword">boolean</span> isMember = parser.parseExpression(<span class="string">&quot;isMember(&#x27;尼古拉·特斯拉&#x27;)&quot;</span>)</span><br><span class="line">               .getValue(context, Boolean.class);</span><br><span class="line">       Assert.assertTrue(isMember);</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>

<h2 id="9-运算符"><a href="#9-运算符" class="headerlink" title="9. 运算符"></a>9. 运算符</h2><h3 id="9-1-关系运算符"><a href="#9-1-关系运算符" class="headerlink" title="9.1. 关系运算符"></a>9.1. 关系运算符</h3><p>关系运算符(等于、不等于、小于、小于或等于、大于、大于或等于)通过使用标准运算符表示法得到支持。下面的清单显示了一些操作符示例:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 运算符：关系运算符</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test11</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 关系运算符(等于、不等于、小于、小于或等于、大于、大于或等于、instanceof、正则表达式)</span></span><br><span class="line">        <span class="comment">// lt (&lt;) gt (&gt;) le (&lt;=) ge (&gt;=) eq (==) ne (!=) div (/) mod (%) not (!) 可以在XML文档中替换相应的符号</span></span><br><span class="line">        <span class="comment">// evaluates to true</span></span><br><span class="line">        <span class="keyword">boolean</span> flag = parser.parseExpression(<span class="string">&quot;2 == 2&quot;</span>).getValue(Boolean.class);</span><br><span class="line">        Assert.assertTrue(flag);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// evaluates to false</span></span><br><span class="line">        flag = parser.parseExpression(<span class="string">&quot;2 &lt; -5.0&quot;</span>).getValue(Boolean.class);</span><br><span class="line">        Assert.assertFalse(flag);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// evaluates to true</span></span><br><span class="line">        flag = parser.parseExpression(<span class="string">&quot;&#x27;black&#x27; &lt; &#x27;block&#x27;&quot;</span>).getValue(Boolean.class);</span><br><span class="line">        Assert.assertTrue(flag);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// evaluates to false</span></span><br><span class="line">        flag = parser.parseExpression(</span><br><span class="line">                <span class="string">&quot;&#x27;xyz&#x27; instanceof T(Integer)&quot;</span>).getValue(Boolean.class);</span><br><span class="line">        Assert.assertFalse(flag);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// evaluates to true</span></span><br><span class="line">        flag = parser.parseExpression(</span><br><span class="line">                <span class="string">&quot;&#x27;5.00&#x27; matches &#x27;^-?\\d+(\\.\\d&#123;2&#125;)?$&#x27;&quot;</span>).getValue(Boolean.class);</span><br><span class="line">        Assert.assertTrue(flag);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//evaluates to false</span></span><br><span class="line">        flag = parser.parseExpression(</span><br><span class="line">                <span class="string">&quot;&#x27;5.0067&#x27; matches &#x27;^-?\\d+(\\.\\d&#123;2&#125;)?$&#x27;&quot;</span>).getValue(Boolean.class);</span><br><span class="line">        Assert.assertFalse(flag);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<p>每个符号运算符也可以指定为纯字母等价的。这避免了使用的符号对嵌入表达式的文档类型具有特殊意义的问题(例如在XML文档中).</p>
<ul>
<li>lt (&lt;)</li>
<li>gt (&gt;)</li>
<li>le (&lt;=)</li>
<li>ge (&gt;=)</li>
<li>eq (==)</li>
<li>ne (!=)</li>
<li>div (/)</li>
<li>mod (%)</li>
<li>not (!)</li>
</ul>
<h3 id="9-2-逻辑运算符"><a href="#9-2-逻辑运算符" class="headerlink" title="9.2. 逻辑运算符"></a>9.2. 逻辑运算符</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 运算符：逻辑运算符</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test12</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        society.getMembers().add(inventorTesla);</span><br><span class="line">        society.getMembers().add(inventorEdlson);</span><br><span class="line">        context.setRootObject(society);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// -- AND --</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// evaluates to false</span></span><br><span class="line">        <span class="keyword">boolean</span> flagAnd = parser.parseExpression(<span class="string">&quot;true and false&quot;</span>).getValue(Boolean.class);</span><br><span class="line">        Assert.assertFalse(flagAnd);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// evaluates to true</span></span><br><span class="line">        String expression = <span class="string">&quot;isMember(&#x27;尼古拉·特斯拉&#x27;) and isMember(&#x27;托马斯·阿尔瓦·爱迪生&#x27;)&quot;</span>;</span><br><span class="line">        flagAnd = parser.parseExpression(expression).getValue(context, Boolean.class);</span><br><span class="line">        Assert.assertTrue(flagAnd);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// -- OR --</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// evaluates to true</span></span><br><span class="line">        <span class="keyword">boolean</span> flagOr = parser.parseExpression(<span class="string">&quot;true or false&quot;</span>).getValue(Boolean.class);</span><br><span class="line">        Assert.assertTrue(flagOr);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// evaluates to true</span></span><br><span class="line">        expression = <span class="string">&quot;isMember(&#x27;尼古拉·特斯拉&#x27;) or isMember(&#x27;托马斯·阿尔瓦·爱迪生&#x27;)&quot;</span>;</span><br><span class="line">        flagOr = parser.parseExpression(expression).getValue(context, Boolean.class);</span><br><span class="line">        Assert.assertTrue(flagOr);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// -- NOT --</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// evaluates to false</span></span><br><span class="line">        <span class="keyword">boolean</span> flagNot = parser.parseExpression(<span class="string">&quot;!true&quot;</span>).getValue(Boolean.class);</span><br><span class="line">        Assert.assertFalse(flagNot);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// -- AND and NOT --</span></span><br><span class="line">        expression = <span class="string">&quot;isMember(&#x27;尼古拉·特斯拉&#x27;) and !isMember(&#x27;托马斯·阿尔瓦·爱迪生&#x27;)&quot;</span>;</span><br><span class="line">        <span class="keyword">boolean</span> flagAndNot = parser.parseExpression(expression).getValue(context, Boolean.class);</span><br><span class="line">        Assert.assertFalse(flagAndNot);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h3 id="9-3-赋值运算符"><a href="#9-3-赋值运算符" class="headerlink" title="9.3. 赋值运算符"></a>9.3. 赋值运算符</h3><p>若要设置属性，请使用赋值运算符(=)。这通常在对setValue的调用中完成，但也可以在对getValue的调用中完成。下面的代码展示了使用赋值运算符的两种方法:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">  * 运算符：赋值运算符</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line"> <span class="meta">@Test</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test14</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    Inventor inventor = <span class="keyword">new</span> Inventor();</span><br><span class="line"></span><br><span class="line">    parser.parseExpression(<span class="string">&quot;Name&quot;</span>).setValue(context, inventor, <span class="string">&quot;Alexander Graham&quot;</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// alternatively</span></span><br><span class="line">    String bell = parser.parseExpression(</span><br><span class="line">        <span class="string">&quot;Name = &#x27;Alexander Graham Bell&#x27;&quot;</span>).getValue(context, inventor, String.class);</span><br><span class="line"></span><br><span class="line">    Assert.assertEquals(<span class="string">&quot;Alexander Graham Bell&quot;</span>, bell);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="10-构造函数"><a href="#10-构造函数" class="headerlink" title="10. 构造函数"></a>10. 构造函数</h2><p>您可以通过使用新的操作符来调用构造函数。除了基本类型(int、float等)和字符串之外，应该对所有类型使用全限定类名。下面的例子展示了如何使用新的操作符来调用构造函数:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 类类型表达式：使用&quot;T(Type)&quot;来表示java.lang.Class实例，&quot;Type&quot;必须是类全限定名，&quot;java.lang&quot;包除外，即该包下的类可以不指定包名；</span></span><br><span class="line"><span class="comment">     * 使用类类型表达式还可以进行访问类静态方法及类静态字段。</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test15</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        Inventor einstein = parser.parseExpression(</span><br><span class="line">                <span class="string">&quot;new com.littlefxc.examples.spel.Inventor(&#x27;爱因斯坦&#x27;, &#x27;德国&#x27;)&quot;</span>)</span><br><span class="line">                .getValue(Inventor.class);</span><br><span class="line">        Assert.assertEquals(<span class="keyword">new</span> Inventor(<span class="string">&quot;爱因斯坦&quot;</span>, <span class="string">&quot;德国&quot;</span>).getName(), einstein.getName());</span><br><span class="line"></span><br><span class="line">        context.setRootObject(society);</span><br><span class="line">        <span class="comment">//create new inventor instance within add method of List</span></span><br><span class="line">        parser.parseExpression(<span class="string">&quot;Members.add(new com.littlefxc.examples.spel.Inventor(&#x27;爱因斯坦&#x27;, &#x27;德国&#x27;))&quot;</span>).getValue(context);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h2 id="11-变量定义及引用"><a href="#11-变量定义及引用" class="headerlink" title="11. 变量定义及引用"></a>11. 变量定义及引用</h2><p>可以使用#variableName语法引用表达式中的变量。变量是通过在EvaluationContext实现上使用setVariable方法设置的。下面的例子展示了如何使用变量:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 变量定义及引用</span></span><br><span class="line"><span class="comment">     * &lt;p&gt;</span></span><br><span class="line"><span class="comment">     * 变量定义通过 EvaluationContext 接口的 setVariable(variableName, value) 方法定义；&lt;br&gt;</span></span><br><span class="line"><span class="comment">     * 在表达式中使用 &quot;#variableName&quot; 引用；&lt;br&gt;</span></span><br><span class="line"><span class="comment">     * 除了引用自定义变量，SpEL还允许引用根对象及当前上下文对象，使用 &quot;#root&quot; 引用根对象，使用 &quot;#this&quot; 引用当前上下文对象；&lt;br&gt;</span></span><br><span class="line"><span class="comment">     * &lt;/p&gt;</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test16</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        Inventor tesla = <span class="keyword">new</span> Inventor(<span class="string">&quot;Nikola Tesla&quot;</span>, <span class="string">&quot;Serbian&quot;</span>);</span><br><span class="line"></span><br><span class="line">        EvaluationContext context = SimpleEvaluationContext.forReadWriteDataBinding().build();</span><br><span class="line">        context.setVariable(<span class="string">&quot;newName&quot;</span>, <span class="string">&quot;Mike Tesla&quot;</span>);</span><br><span class="line"></span><br><span class="line">        parser.parseExpression(<span class="string">&quot;Name = #newName&quot;</span>).getValue(context, tesla);<span class="comment">// &quot;Mike Tesla&quot;</span></span><br><span class="line"><span class="comment">//        parser.parseExpression(&quot;#root.Name = #newName&quot;).getValue(context, tesla);// &quot;Mike Tesla&quot;</span></span><br><span class="line">        Assert.assertEquals(<span class="string">&quot;Mike Tesla&quot;</span>, tesla.getName());</span><br><span class="line"></span><br><span class="line">        <span class="comment">// create an array of integers</span></span><br><span class="line">        List&lt;Integer&gt; primes = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">        primes.addAll(Arrays.asList(<span class="number">2</span>, <span class="number">3</span>, <span class="number">5</span>, <span class="number">7</span>, <span class="number">11</span>, <span class="number">13</span>, <span class="number">17</span>));</span><br><span class="line"></span><br><span class="line">        <span class="comment">// create parser and set variable &#x27;primes&#x27; as the array of integers</span></span><br><span class="line">        context.setVariable(<span class="string">&quot;primes&quot;</span>, primes);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 通过使用诸如 (using selection ?&#123;...&#125;) 这样的选择表达式，选择列表中所有大于10的数字</span></span><br><span class="line">        <span class="comment">// evaluates to [11, 13, 17]</span></span><br><span class="line">        List&lt;Integer&gt; primesGreaterThanTen = (List&lt;Integer&gt;) parser</span><br><span class="line">                .parseExpression(<span class="string">&quot;#primes.?[#this&gt;10]&quot;</span>).getValue(context);</span><br><span class="line"></span><br><span class="line">        System.out.println(primesGreaterThanTen);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h2 id="12-自定义函数"><a href="#12-自定义函数" class="headerlink" title="12. 自定义函数"></a>12. 自定义函数</h2><p>您可以通过注册可在表达式字符串中调用的用户定义函数来扩展SpEL。 该功能通过EvaluationContext注册。 以下示例显示如何注册用户定义的函数：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 自定义函数</span></span><br><span class="line"><span class="comment">     * &lt;p&gt;</span></span><br><span class="line"><span class="comment">     * 目前只支持类静态方法注册为自定义函数；&lt;br&gt;</span></span><br><span class="line"><span class="comment">     * SpEL使用StandardEvaluationContext的registerFunction方法进行注册自定义函数，</span></span><br><span class="line"><span class="comment">     * 其实完全可以使用setVariable代替，两者其实本质是一样的；</span></span><br><span class="line"><span class="comment">     * &lt;/p&gt;</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test17</span><span class="params">()</span> <span class="keyword">throws</span> NoSuchMethodException </span>&#123;</span><br><span class="line">        Method parseInt = Integer.class.getDeclaredMethod(<span class="string">&quot;parseInt&quot;</span>, String.class);</span><br><span class="line">        <span class="comment">// 自定义函数推荐用 context.registerFunction(&quot;fnName&quot;, fn)</span></span><br><span class="line">        context.registerFunction(<span class="string">&quot;parseInt&quot;</span>, parseInt);</span><br><span class="line">        context.setVariable(<span class="string">&quot;parseInt2&quot;</span>, parseInt);</span><br><span class="line">        Boolean bool = parser.parseExpression(<span class="string">&quot;#parseInt(&#x27;3&#x27;) == #parseInt2(&#x27;3&#x27;)&quot;</span>).getValue(context, Boolean.class);</span><br><span class="line">        Assert.assertTrue(bool);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h2 id="13-三目运算及Elivis运算表达式"><a href="#13-三目运算及Elivis运算表达式" class="headerlink" title="13. 三目运算及Elivis运算表达式"></a>13. 三目运算及Elivis运算表达式</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 三目运算及Elivis运算表达式</span></span><br><span class="line"><span class="comment"> * &lt;br&gt;</span></span><br><span class="line"><span class="comment"> * 三目运算符 &quot;表达式1?表达式2:表达式3&quot;用于构造三目运算表达式，如&quot;2&gt;1?true:false&quot;将返回true；</span></span><br><span class="line"><span class="comment"> * &lt;br&gt;</span></span><br><span class="line"><span class="comment"> * Elivis运算符 &quot;表达式1?:表达式2&quot; 从Groovy语言引入用于简化三目运算符的，</span></span><br><span class="line"><span class="comment"> * 当 表达式1 为 非null 时则返回 表达式1，</span></span><br><span class="line"><span class="comment"> * 当 表达式1 为 null 时则返回 表达式2，</span></span><br><span class="line"><span class="comment"> * 简化了三目运算符方式 &quot;表达式1?表达式1:表达式2&quot;，如 &quot;null?:false&quot; 将返回 false，而 &quot;true?:false&quot; 将返回true；</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test18</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">int</span> int1 = parser.parseExpression(<span class="string">&quot;true and false ? 1 : 0&quot;</span>).getValue(Integer.class);</span><br><span class="line">    Assert.assertEquals(<span class="number">0</span>, int1);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 如果是 null, 返回 false</span></span><br><span class="line">    Boolean bool1 = parser.parseExpression(<span class="string">&quot;null ?: false&quot;</span>).getValue(Boolean.class);</span><br><span class="line">    Assert.assertFalse(bool1);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// true 不是 null, 返回 true</span></span><br><span class="line">    Boolean bool2 = parser.parseExpression(<span class="string">&quot;true ?: false&quot;</span>).getValue(Boolean.class);</span><br><span class="line">    Assert.assertTrue(bool2);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 稍微复杂点</span></span><br><span class="line">    Inventor tesla = <span class="keyword">new</span> Inventor(<span class="string">&quot;Nikola Tesla&quot;</span>, <span class="string">&quot;Serbian&quot;</span>);</span><br><span class="line">    String name = parser.parseExpression(<span class="string">&quot;Name?:&#x27;Elvis Presley&#x27;&quot;</span>).getValue(context, tesla, String.class);</span><br><span class="line">    Assert.assertEquals(<span class="string">&quot;Nikola Tesla&quot;</span>, name);  <span class="comment">// Nikola Tesla</span></span><br><span class="line"></span><br><span class="line">    tesla.setName(<span class="keyword">null</span>);</span><br><span class="line">    name = parser.parseExpression(<span class="string">&quot;Name?:&#x27;Elvis Presley&#x27;&quot;</span>).getValue(context, tesla, String.class);</span><br><span class="line">    Assert.assertEquals(<span class="string">&quot;Elvis Presley&quot;</span>, name);  <span class="comment">// Elvis Presley</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 可以把Elivis运算表达式作为 默认值 例如 : @Value(&quot;#&#123;systemProperties[&#x27;pop3.port&#x27;] ?: 25&#125;&quot;)</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="14-安全的导航操作符"><a href="#14-安全的导航操作符" class="headerlink" title="14. 安全的导航操作符"></a>14. 安全的导航操作符</h2><p>安全导航操作符用于避免NullPointerException并来自Groovy语言。 通常，在引用对象时，可能需要在访问对象的方法或属性之前验证它是否为null。 为避免这种情况，安全导航操作符返回null而不是抛出异常。 以下示例显示如何使用安全导航运算符：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 安全的导航操作符：</span></span><br><span class="line"><span class="comment">     * 安全导航操作符用于避免NullPointerException，它来自Groovy语言。</span></span><br><span class="line"><span class="comment">     * 通常，当您有一个对象的引用时，您可能需要在访问该对象的方法或属性之前验证它是否为null。为了避免这种情况，安全导航操作符返回null，而不是抛出异常。</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test19</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        Inventor tesla = <span class="keyword">new</span> Inventor(<span class="string">&quot;Nikola Tesla&quot;</span>, <span class="string">&quot;Serbian&quot;</span>);</span><br><span class="line">        tesla.setPlaceOfBirth(<span class="keyword">new</span> PlaceOfBirth(<span class="string">&quot;Smiljan&quot;</span>));</span><br><span class="line"></span><br><span class="line">        String city = parser.parseExpression(<span class="string">&quot;PlaceOfBirth?.City&quot;</span>).getValue(context, tesla, String.class);</span><br><span class="line">        Assert.assertEquals(<span class="string">&quot;Smiljan&quot;</span>, city);  <span class="comment">// Smiljan</span></span><br><span class="line"></span><br><span class="line">        tesla.setPlaceOfBirth(<span class="keyword">null</span>);</span><br><span class="line">        city = parser.parseExpression(<span class="string">&quot;PlaceOfBirth?.City&quot;</span>).getValue(context, tesla, String.class);</span><br><span class="line">        Assert.assertNull(city);  <span class="comment">// null - 没有抛出空指针异常(NullPointerException)!!!</span></span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h2 id="15-集合选择-Collection-Selection"><a href="#15-集合选择-Collection-Selection" class="headerlink" title="15. 集合选择(Collection Selection)"></a>15. 集合选择(Collection Selection)</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 集合选择(Collection Selection)：</span></span><br><span class="line"><span class="comment">     * 选择表达式允许通过从源集合的条目中进行选择，将源集合转换为另一个集合。</span></span><br><span class="line"><span class="comment">     * 选择表达式通过使用形如 &quot;.?[selectionExpression]&quot; 的语法。它筛选集合并返回包含原始元素子集的新集合。</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test20</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        society.getMembers().addAll(inventorList);</span><br><span class="line">        context.setRootObject(society);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 从列表中选出国籍是美国的元素，然后组成新的列表</span></span><br><span class="line">        List&lt;Inventor&gt; list = (List&lt;Inventor&gt;) parser</span><br><span class="line">                .parseExpression(<span class="string">&quot;Members.?[Nationality == &#x27;塞尔维亚裔美籍&#x27;]&quot;</span>)</span><br><span class="line">                .getValue(context);</span><br><span class="line">        Assert.assertEquals(<span class="number">1</span>, list.size()); <span class="comment">// 只有特斯拉是塞尔维亚裔美籍</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 从原字典中选出值大于27的元素，然后组成新的字典</span></span><br><span class="line">        Map&lt;String, Integer&gt; map = <span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line">        map.put(<span class="string">&quot;a&quot;</span>, <span class="number">26</span>);</span><br><span class="line">        map.put(<span class="string">&quot;b&quot;</span>, <span class="number">27</span>);</span><br><span class="line">        map.put(<span class="string">&quot;c&quot;</span>, <span class="number">28</span>);</span><br><span class="line">        context.setVariable(<span class="string">&quot;map&quot;</span>, map);</span><br><span class="line">        Map newMap = (Map) parser.parseExpression(<span class="string">&quot;#map.?[value&lt;27]&quot;</span>).getValue(context);</span><br><span class="line">        Assert.assertEquals(<span class="number">1</span>, newMap.size());</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h2 id="16-集合投影-Collection-Projection"><a href="#16-集合投影-Collection-Projection" class="headerlink" title="16. 集合投影(Collection Projection)"></a>16. 集合投影(Collection Projection)</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 集合投影:</span></span><br><span class="line"><span class="comment">     * 投影让集合驱动子表达式的求值，结果是一个新的集合。</span></span><br><span class="line"><span class="comment">     * 投影的语法是 &quot;.![projectionExpression]&quot;。例如，假设我们有一个发明家列表，但是想要他们出生的城市列表。实际上，我们想要的是“出生地点”。</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test21</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        society.getMembers().addAll(inventorList);</span><br><span class="line">        context.setRootObject(society);</span><br><span class="line"></span><br><span class="line">        List&lt;String&gt; placesOfBirth = (List) parser.parseExpression(<span class="string">&quot;Members.![placeOfBirth.city]&quot;</span>).getValue(context);</span><br><span class="line">        MatcherAssert.assertThat(placesOfBirth, IsIterableContainingInOrder.contains(<span class="string">&quot;利卡-塞尼县&quot;</span>, <span class="string">&quot;米兰&quot;</span>, <span class="string">&quot;爱丁堡&quot;</span>));</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h2 id="17-引用"><a href="#17-引用" class="headerlink" title="17. 引用"></a>17. 引用</h2><p><a target="_blank" rel="noopener" href="https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#expressions">Spring Expression Language (SpEL)</a></p>
<p><a target="_blank" rel="noopener" href="https://jinnianshilongnian.iteye.com/blog/1418309">https://jinnianshilongnian.iteye.com/blog/1418309</a></p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/blog/tags/spring/" rel="tag"># spring</a>
              <a href="/blog/tags/spring-expression/" rel="tag"># spring-expression</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/blog/2019/05/06/spring-batch%E5%85%A5%E9%97%A8%E4%B9%8BCSV-to-DB/" rel="prev" title="spring-batch入门之CSV-to-DB">
                  <i class="fa fa-chevron-left"></i> spring-batch入门之CSV-to-DB
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/blog/2019/05/06/%E6%97%A5%E5%BF%97%E5%86%99%E5%85%A5%E6%95%B0%E6%8D%AE%E5%BA%93Log4j2-JDBCAppender/" rel="next" title="日志写入数据库Log4j2-JDBCAppender">
                  日志写入数据库Log4j2-JDBCAppender <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>







<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">一年春又来</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/mist/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/blog/js/utils.js"></script><script src="/blog/js/motion.js"></script><script src="/blog/js/schemes/muse.js"></script><script src="/blog/js/next-boot.js"></script>

  
<script src="/blog/js/local-search.js"></script>






  





</body>
</html>
